﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace DAL
{
   public class Connect
    {
           /// <summary>
           /// Chuỗi kết nối tới CSDL
           /// </summary>
           private string strcnn = @"Data Source=" + CauHinhHeThong.Default.Sever + ";Initial Catalog=" + CauHinhHeThong.Default.DataBaseName + ";Integrated Security=True";
           /// <summary>
           /// CNN SQLcommandconnection
           /// Thuộc tính protected
           /// </summary>
           /// 
           private void khoitao()
           {
               if (DAL.CauHinhHeThong.Default.TrangThai)
               {
                   strcnn = @"Data Source=" + CauHinhHeThong.Default.Sever + ";Initial Catalog=" + CauHinhHeThong.Default.DataBaseName + ";User ID=" + CauHinhHeThong.Default.UserName + ";Password=" + CauHinhHeThong.Default.PassWord + ";";
               }
               else
               {
                   strcnn = @"Data Source=" + CauHinhHeThong.Default.Sever + ";Initial Catalog=" + CauHinhHeThong.Default.DataBaseName + ";Integrated Security=True";
               }
           }
           protected SqlConnection cnn;
           /// <summary>
           /// Mở đường kết nối tới CSDL
           /// </summary>
           protected void cnnopen()
           {
               khoitao();
               String strCnn = strcnn;
               cnn = new SqlConnection(strCnn);
               cnn.Open();
               CauHinhHeThong.Default.Solantruyxuat++;
               CauHinhHeThong.Default.Save();
           }
           /// <summary>
           /// Đóng đường kết nối
           /// </summary>
           /// <remarks>Design by sokhanh03</remarks>
           protected void cnnclose()
           {
               cnn.Close();
           }
           public void settingwindows(string severname, string database)
           {
               CauHinhHeThong.Default.Sever = severname;
               CauHinhHeThong.Default.DataBaseName = database;
               CauHinhHeThong.Default.Save();
               CauHinhHeThong.Default.Reload();
           }
           public void settingsql(string severname, string database, string user, string pass)
           {
               CauHinhHeThong.Default.Sever = severname;
               CauHinhHeThong.Default.DataBaseName = database;
               CauHinhHeThong.Default.UserName = user;
               CauHinhHeThong.Default.PassWord = pass;
               CauHinhHeThong.Default.Save();
               CauHinhHeThong.Default.Reload();
           }
           public void test()
           {
               CauHinhHeThong.Default.Reload();
               khoitao();
               string strCnn = strcnn;
               cnn = new SqlConnection(strCnn);
               cnn.Open();
               cnn.Close();
           }
           public int solantruyxuatcsdl()
           {
               CauHinhHeThong.Default.Reload();
               return CauHinhHeThong.Default.Solantruyxuat;
           }
           public void resetsolantruycap()
           {
               CauHinhHeThong.Default.Solantruyxuat = 0;
               CauHinhHeThong.Default.Save();
           }

       }
        
}
